﻿var isShowLegend = false;
var isShowBasemap = false;
var modelURL = getBaseplatformPort();
function showMyMap(){
    $("#myMapIframe").show();
    $("#myMapIframe").attr("src", "/MapServer/Portal/myMapLayer.html");
}

function showLegend() {
    var layerIds = [];
    var allLegends = [];
    var oneLegend;
    var imageData, label;
    var legendHtml = "";
    var LegendDict = null;
    if (isShowLegend) {
        $('#legendDiv').css('display', 'none');
        isShowLegend = false;
    } else {
        $('#legendDiv').css('display', 'block');
        isShowLegend = true;
    }
    if (isShowLegend) {
        for (var attr in cesium.layer3DList) {
            var layerItem = cesium.layer3DList[attr];
            if (layerItem["NAME"] && layerItem["SERVICETYPE"] != sw_servicetype.sw_terrain && layerItem["SERVICETYPE"] != sw_servicetype.sw_oblique && layerItem["SOURCE"] != sw_source.sw_mongodb) {
                layerIds.push(attr);
            }
        }
        if (layerIds.length > 0) {
            for (var i = 0; i < layerIds.length; i++) {
                var lengendVals = "(";
                var layerItem = cesium.layer3DList[layerIds[i]];
                var layerUrl = layerItem["URL"];
                switch (layerItem["SERVICETYPE"]) {
                    case sw_servicetype.sw_tile: //tile
                        switch (layerItem["SOURCE"]) {
                            case sw_source.sw_arcgis: //arcgis
                            case sw_source.sw_arcgistile: //arcgis
                                oneLegend = getLegendForArcgis(layerUrl);
                                allLegends.push(oneLegend);
                                for (var key in oneLegend.layers) {
                                    var layerName = oneLegend.layers[key].layerName;
                                    if(oneLegend.drawfiled != undefined){
                                        legendHtml += '<div style = "margin-top:10px;margin-left:10px;font-size: 16px;">' + layerItem.NAME + '  符号化字段：' + oneLegend.drawfiled + '</div>'
                                    }else{
                                        legendHtml += '<div style = "margin-top:10px;margin-left:10px;font-size: 16px;">' + layerItem.NAME + '</div>'
                                    }
                                    var legend = oneLegend.layers[key].legend;
                                    if (legend.length > 0) {
                                        if(oneLegend.drawfiled == "DLBM"){
                                            for (var j in legend) {
                                                label = legend[j].label;
                                                if(j == legend.length-1){
                                                    lengendVals += "'"+ label +"')";

                                                }else{
                                                    lengendVals += "'"+ label +"',";
                                                }
                                            }
                                            var results = sendAjax(modelURL + "businessDic/getBusinessDicByValue", "POST", {
                                                values:lengendVals
                                            });
                                            if (results.code == 200) {
                                                LegendDict = results.data;
                                            } else {
                                                layer.msg('获取图例失败', {icon: 5});
                                            }
                                            for (var k in legend) {
                                                imageData = legend[k].imageData;
                                                label = legend[k].label;
                                                var value = null;
                                                for(var key in LegendDict){
                                                    if(LegendDict[key][label] != undefined){
                                                        value = LegendDict[key][label];
                                                    }
                                                }
                                                if(value == null){
                                                    value = label;
                                                }
                                                legendHtml += '<div class="layui-form-item" style = "margin: 5px 40px;font-size: 14px;"><img src="data:image/png;base64,' + imageData + '"style="margin: 0px 10px;">';
                                                legendHtml += '<div class="layui-input-block" style = "min-height: 36px;display: inline-block !important;">' + value + '</div></div>';
                                            }
                                        }else{
                                            for (var x in legend) {
                                                imageData = legend[x].imageData;
                                                label = legend[x].label;
                                                legendHtml += '<div class="layui-form-item" style = "margin: 5px 40px;font-size: 14px;"><img src="data:image/png;base64,' + imageData + '"style="margin: 0px 10px;">';
                                                legendHtml += '<div class="layui-input-block" style = "min-height: 36px;display: inline-block !important;">' + label + '</div></div>';
                                            
                                            }
                                        }
                                    }
                                }
                                break;
                            default:
                                break;
                        }
                        break;
                    case sw_servicetype.vector: //vector
                        break;
                    default:
                        break;
                }
            }
        } else {
            legendHtml += "<div style='width: 100%;height: 100%;text-align: center;'>当前场景中没有支持显示图例的数据！</div>";
        }
        $('#legendContainer').html(legendHtml);
    }
    $('#btn-cancel-legend').click(function() {
        $('#legendDiv').css('display', 'none');
        isShowLegend = false;
    })
}
//获取图例
function getLegendForArcgis(positionURL) {
    var url = positionURL + "/legend?f=json";
    var thisResult = null;
    $.ajax({
        type: "post",
        async: false,
        url: url,
        data: {},
        datatype: "json",
        success: function(result) {
            // thisResult = JSON.parse(result);
            thisResult = result;

        },
        error: function(err) {}
    });
    url = positionURL + "/0?f=json";
    $.ajax({
        type: "post",
        async: false,
        url: url,
        data: {},
        datatype: "json",
        success: function(result) {
            if (result) {
                thisResult["drawfiled"] = result.drawingInfo.renderer.field1;
            }
        },
        error: function(err) {}
    });
    return thisResult;
}

function showBasemapSwitch() {
    if (isShowBasemap) {
        $('#basemapDiv').css('display', 'none');
        isShowBasemap = false;
    } else {
        $('#basemapDiv').css('display', 'block');
        isShowBasemap = true;
    }
    if (isShowBasemap) {
        var html = "";
        html += '<div class="layui-row" style="height:100%"><div title="影像地图" class="layui-col-md6" style="height:70%;text-align: center;"><img style = "width: 80px;height: 70px;margin-top:7px" src="img/toolbar/ordosbasemap1.png"></div>';
        html += '<div title="电子地图" class="layui-col-md6" style="height:70%;text-align: center;"><img style = "width: 80px;height: 70px;margin-top:7px" src="img/toolbar/ordosbasemap.png"></div></div>'
        $('#basemapContainer').html(html);
    }
    $('#btn-cancel-basemap').click(function() {
        $('#basemapDiv').css('display', 'none');
        isShowBasemap = false;
    })
}
//请求接口
function sendAjax(url, type, data) {
    var results;
    $.ajax({
        type: type,
        dataType: "json",
        url: url,
        data: data,
        success: function(data) {
            results = data;
        },
        error: function(error) {
            results = error;
        }
    });
    return results;
}